<?php
class Model_FriendRequest extends Df_Model{
	
	private $user_id;

	public function __construct($id = null) {
		$this->user_id = $id;
		parent::__construct(new Model_DbTable_UsersRequest(), $id);
		
	}
	
	public function declineRequest($request_id) {
		$this->removeRequest($request_id);
	}
	
	public function approveRequest($request_id, $user_id) {
		//check if that real user requests, not other
		$user_request = $this->getInfoRequestId($request_id);
		if($user_request->user_request == $user_id){

			//add new user to table 
			$friend = new Model_Friend();
			$friend->user_id = $user_request->user_id;
			$friend->user_friend =  $user_id;
			$friend->save();
			
			$this->removeRequest($request_id);
			return true;
		}else{
			return false;
		}
	}
	
	private function removeRequest($request_id){
		$this->_dbTable->find($request_id)->current()->delete();
	}
	
	public function checkIfSend($user_id, $user_request) {
		return $this->_dbTable->fetchAll(
			$this->_dbTable->select()
				->where('user_id = ?',$user_id)
				->where('user_request = ?',$user_request)
		)->count();
	}
	
	public function getRequests() {
		return $this->_dbTable->fetchAll(
			$this->_dbTable->select()
				->where('user_request = ?',$this->user_id)
		);
	}
	
	public function getInfoRequestId($request_id) {
		return $this->_dbTable->fetchRow(
			$this->_dbTable->select()
				->where('id = ?',$request_id)
		);
	}

	
}